package com.yltrcc.blog.controller.api;

import com.yltrcc.blog.model.model.ApiResponse;
import com.yltrcc.blog.generate.domain.*;
import com.yltrcc.blog.model.enums.ArticleStatus;
import com.yltrcc.blog.business.ArticleService;
import com.yltrcc.blog.business.CategoryService;
import com.yltrcc.blog.model.model.ArticleList;
import com.yltrcc.blog.model.model.ArticleV2List;
import com.yltrcc.blog.model.model.ArticleV3List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * Package: com.yltrcc.blog.api
 * Date：2022-02-03
 * Time：13:21
 * Description：用户获取面试题相关API
 *
 * @author yltrcc
 * @version 1.0
 */
@RestController
@RequestMapping(value = "/api/article")
public class ArticleApiController {


    @Autowired
    private ArticleService articleService;

    @Autowired
    private CategoryService categoryService;


    /**
     * 三级界面 查询出所有分类不包含面试题
     */
    @GetMapping("/queryAllCategoryV3")
    public ApiResponse<ArticleV3List> queryAllCategoryV3() {

        List<ArticleV3List> res = new ArrayList<>();
        //分两步发送请求
        //第一步 查询所有 top 分类
        List<Category> topCategories = categoryService.findCategoryByTop();
        //第一步 通过top分类id 获取下一级分类
        if (!CollectionUtils.isEmpty(topCategories)) {
            for (Category topCategory : topCategories) {
                List<Category> categories = categoryService.findLowerCategoryByCategoryId(topCategory.getCategoryId());
                //第二步 根据下一级分类 获取 子分类
                List<ArticleV2List> v2Lists = new ArrayList<>();
                if (!CollectionUtils.isEmpty(categories)) {
                    for (Category category : categories) {
                        //第三步 根据下一级分类 获取 子分类
                        List<Category> subCategories = categoryService.findLowerCategoryByCategoryId(category.getCategoryId());
                        ArticleV2List questionV2List = new ArticleV2List();
                        List<ArticleList> list = new ArrayList<>();
                        if (!CollectionUtils.isEmpty(subCategories)) {
                            for (Category subCategory : subCategories) {
                                ArticleList articleList = new ArticleList();
                                articleList.setCategoryName(subCategory.getCategoryName());
                                articleList.setCategoryId(subCategory.getCategoryId());
                                list.add(articleList);
                            }
                        }
                        questionV2List.setCategoryName(category.getCategoryName());
                        questionV2List.setData(list);
                        v2Lists.add(questionV2List);
                    }
                }
                ArticleV3List v3List = new ArticleV3List();
                v3List.setCategoryName(topCategory.getCategoryName());
                v3List.setData(v2Lists);
                res.add(v3List);
            }

        }

        ApiResponse<ArticleV3List> response = new ApiResponse<>(res);
        response.setStatus(200);
        response.setCode(200);
        response.setMessage("获取成功！！！");

        return response;
    }

    /**
     * 根据子分类查询 面试题
     * @param categoryId 子分类
     * @return
     */
    @GetMapping("/queryArticleByCategoryId")
    public ApiResponse<ArticleList> queryArticleByCategoryId(@RequestParam("categoryId") Integer categoryId) {

        List<ArticleList> res = new ArrayList<>();
        ArticleList articleList = new ArticleList();
        Category category = new Category();
        category.setCategoryId(categoryId);
        List<BlogArticleModel> data = articleService.findArtileByCategoryId(category, ArticleStatus.PUBLISH.getStatus());
        articleList.setData(data);
        res.add(articleList);
        ApiResponse<ArticleList> response = new ApiResponse<>(res);
        response.setStatus(200);
        response.setCode(200);
        response.setMessage("获取成功！！！");

        return response;
    }



}
